package com.pzhu.acargid.model;

import com.pzhu.acargid.domain.Model;
import com.pzhu.acargid.domain.Recycled;
import com.pzhu.acargid.recycled.RecycledService;
import com.pzhu.acargid.until.Result;
import com.pzhu.security.token.JwtTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/model")
public class ModelController {
    @Autowired
    private ModelService modelService;
    @Autowired
    private RecycledService recycledService;

    @GetMapping("/modules")
    public Result modules(HttpServletRequest request) {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        List<Model> list = modelService.list();
        list.forEach(item -> {
                    modelService.getfiles(userid, item);
                    if (item.getModDbTable().equals("recycled")) {
                        item.setFiles((int) recycledService.count());
                    }
                }
        );
        return Result.success(list);
    }

}
